<?php
namespace Admin\Controller;

use Think\Controller;
use Admin\Model\MemberModel;
use Admin\Model\RegionModel;
use Admin\Model\CityModel;
use Admin\Model\ProvinceModel;

/**
 * 运营中心后台城市合伙人类
 */
class PartnerController extends Controller
{

	function __construct(){
        parent::__construct();
        $this->Member = new MemberModel();
	}

	/**
	 * 获取城市合伙人列表
	 * @param int $offset 分页从第几条开始
	 * @param int $limit 每页条数
	 */
	public function get_member_list($offset=0,$limit=10){
		$member=$this->Member->get_member_list($offset,$limit,1);
		if(!$member)
			returnMobileJson(201,'没有数据');
		else
			returnMobileJson(200,'获取成功',$member);
	}

	/**
	 * 展示添加合伙人省份
	 * @return string 返回开放省份
	 */
	public function get_member_province(){
		$Province=new ProvinceModel();
		$province=$Province->get_province();
		returnMobileJson(200,'获取成功',$province);
	}

	/**
	 * 展示添加合伙人城市
	 * @return string 返回开放城市
	 */
	public function get_member_city(){
		$pid=$_POST['pid'];
		if(empty($pid)){
			returnMobileJson(201,'参数错误');
		}
		$City=new CityModel();
		$city=$City->get_city($pid);
		returnMobileJson(200,'获取成功',$city);
	}

	/**
	 * 添加城市合伙人
	 * @param $m_account 默认为手机号
	 */
	public function add_member(){
		// 接收城市和密码并验证是否为空
		$city_id = $_POST['city_id'];
		$m_pwd = $_POST['m_pwd'];
		if(empty($city_id) || empty($m_pwd)){
			returnMobileJson(201,'参数错误');
		}
		//接收账号并验证是否为手机号码
		$m_account = $_POST['m_account'];
		if(!preg_match("/^1\d{10}$/",$m_account)){
			returnMobileJson(202,'参数错误,账号需为手机号');
		}

		if($this->Member->get_exist_member($m_account)){
			returnMobileJson(203,'账号已存在');
		}

		$data = array('city_id' => $city_id, 'm_account'=>$m_account, 'm_pwd'=> md5($m_pwd), 'm_type'=>1, 'm_date' => date('Y-m-d H:i:s'));
		
		if($this->Member->set_member($data)){
			returnMobileJson(200,'添加成功');		
		}else{
			returnMobileJson(204,'添加失败');
		}
	}

	/**
	 * 获取城市合伙人信息
	 * @param data 合伙人的详细信息
	 */
	public function get_member(){
		$member_id=$_REQUEST['member_id'];
		if(empty($member_id)){
			returnMobileJson(201,'参数错误');
		}
		// 基本信息
		$data['basic_info']=$this->Member->get_member($member_id);
//		print_r($data['basic_info']);
//		if($data['basic_info']['m_type'] != 1){
//			returnMobileJson(201,'此人不是城市合伙人');
//		}
		$Region=new RegionModel();
		$data['region']=$Region->get_region($data['basic_info']['city_id']);
		// 联盟商户
		$data['union_business']=$this->Member->get_member_list(0,null,2,$member_id);
		// 自有工程师
		$data['have_engineer']=$this->Member->get_member_list(0,null,3,$member_id);
		// 滴滴工程师
		$data['didi_engineer']=$this->Member->get_member_list(0,null,4,$member_id);
		returnMobileJson(200,'获取成功',$data);		
	}

	/**
	 * 修改城市合伙人信息
	 * @param [type] [description]
	 */
	public function edit_member(){
		$member_id=$_POST['member_id'];
		$city_id=$_POST['city_id'];
		$m_name = $_POST['m_name'];
		$m_status=$_POST['m_status'];
//		$m_account=$_POST['m_account'];
		$m_pwd=$_POST['m_pwd'];
		$m_settlement_cycle=$_POST['m_settlement_cycle'];
		$m_royalty_ratio=$_POST['m_royalty_ratio'];
		if(empty($member_id)){
			returnMobileJson(201,'参数错误');
		}
//		if($this->Member->get_exist_member($m_account)){
//			returnMobileJson(202,'账号已存在');
//		}

		$data=array('m_date' => date('Y-m-d H:i:s'));

		$city_id && $data['city_id'] = $city_id;
		$m_pwd && $data['m_pwd'] = md5($m_pwd);
		($m_status=='0' || $m_status=='1') && $data['m_status'] = $m_status;
		$m_settlement_cycle && $data['m_settlement_cycle'] = $m_settlement_cycle;
		$m_name && $data['m_name'] = $m_name;
		$m_royalty_ratio && $data['m_royalty_ratio'] = $m_royalty_ratio;

		if($data['m_royalty_ratio']){
			if(is_numeric($data['m_royalty_ratio'])){
				if($data['m_royalty_ratio']<100 && $data['m_royalty_ratio']>=0){

				}else{
					returnMobileJson(-2,'错误提成比例');
				}
			}else{
				returnMobileJson(-2,'错误提成比例');
			}
		}



		if($this->Member->set_member($data,$member_id)){
			returnMobileJson(200,'修改成功');		
		}else{
			returnMobileJson(203,'修改失败');
		}
	}


	/**
	 * 城市合伙人登录
	 */
	public function city_admin_login(){
		$phone = $_REQUEST['phone'];
		$password = $_REQUEST['password'];
		$type = $_REQUEST['type'];
		$map = array();
		$map['m_account'] = $phone;
		if($this->Member->get_exist_member($phone)){
			$data = $this->Member->get_member_by_m_account($phone);
//			print_r($data);
			if(($data['m_type']==1 && !$type) || ($data['m_type']==2 && $type==2)){
				if($data['m_pwd']==md5($password)){
					$_SESSION['member_id'] = $data['member_id'];
					$_SESSION['m_type'] =  $data['m_type'];
					$_SESSION['admin_id'] = 1;
					returnMobileJson(200,'登录成功',$data);
				}else{
					returnMobileJson(-2,'密码错误');
				}
			}else{
				returnMobileJson(-2,'账号不存在');
			}

		}else{
			returnMobileJson(-2,'账号不存在');
		}
	}


	/**
	 * 运营中心登录
	 */
	public function admin_login(){
		$phone = $_REQUEST['phone'];
		$password = $_REQUEST['password'];
		if($phone=='18779862403' && $password=='123123'){
			$_SESSION['member_id'] = 0;
			$_SESSION['admin_id'] = 1;
 			returnMobileJson(200,'登录成功');
		}else{
			returnMobileJson(-2,'密码错误');
		}
	}

	/**
	 * 登录退出接口
	 */
	public function exit_login(){
		$_SESSION['member_id'] = '';
		$_SESSION['admin_id'] = '';
		$_SESSION['m_type'] = '';
		$_SESSION['uid'] = '';
		$_SESSION['administrator_id'] = '';
		returnMobileJson(200,'退出登录成功');
	}

	/**
	 * 超级接口获取账号信息
	 */
	public function get_admin_info(){
		if($_SESSION['member_id']>=0){
			if($_SESSION['member_id']>0){
				$data = $this->Member->get_member($_SESSION['member_id']);
			}else{
				$data['m_type'] = 0;
				$data['m_name'] = 'admin';
			}

			returnMobileJson(200,'获取成功',$data);
		}else{
			returnMobileJson(0,'未登录');
		}
	}

	/**
	 * 微信模拟登陆
	 */
	public function super_wx_login($master_id = 0){
		$_SESSION['master_id'] = $master_id;
		$_SESSION['admin_id'] = $master_id;
		returnMobileJson(200,'获取成功',array('master_id'=>$master_id));
	}
}